<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="名称"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="业务类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="业务类型" clearable size="small">
          <el-option
            v-for="(dict,index) in options"
            :key="index"
            :label="dict.name"
            :value="dict.abbreviation"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd()"
        >添加
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="list">
      <el-table-column type="selection" align="center" width="50" />
      <el-table-column label="内部名称" align="center" prop="name" />
      <el-table-column label="业务类型" align="center" prop="type_name">
        <template v-slot="scope">
          <span>{{ scope.row.type_name + '('+scope.row.type+')' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button
            size="mini"
            type="primary"
            @click="handleEdit(scope.row)"
          >编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDel(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 新增/修改 -->
    <AddEdit :open.sync="open" :title="title" :form="form" :options="options" @getList="getList" />
  </div>

</template>
<script>
import { insideTypeList, insideTypeDel, getBusinessType } from '@/api/basics'
import AddEdit from './AddEdit'
export default {
  name: 'Index',
  components: {
    AddEdit
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      open: false,
      title: '',
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      form: {},
      // 查询参数
      queryParams: {
        page: 1,
        limit: 10,
        name: undefined,
        type: undefined
      },
      options: []
    }
  },
  created() {
    this.getList()
    this.BusinessType()
  },
  methods: {
    /** 查询列表 */
    getList() {
      const that = this
      this.loading = true
      insideTypeList(this.queryParams).then(response => {
        if (response.code === 10000) {
          that.list = response.data
          that.total = response.count
          that.loading = false
        }
      })
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.form = {}
      this.open = true
      this.title = '添加'
    },
    /** 编辑按钮操作 */
    handleEdit(row) {
      this.form = row
      this.open = true
      this.title = '编辑'
    },
    /** 删除按钮操作 */
    handleDel(row) {
      const that = this
      that.$confirm('是否要删除？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        that.loading = true
        insideTypeDel({ id: row.id.toString() }).then(response => {
          that.loading = false
          if (response.code === 10000) {
            that.$message.success(response.message)
            that.getList()
          } else {
            that.$message.error(response.message)
          }
        })
      }).catch(() => {
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        page: 1,
        limit: 10,
        name: undefined,
        type: undefined
      }
      this.handleQuery()
    },
    BusinessType() {
      const that = this
      getBusinessType().then(response => {
        if (response.code === 10000) {
          that.options = response.data
        }
      })
    }
  }
}
</script>

